package cn.yougou.serviceImpl;

import cn.yougou.mapper.YgCategoryMapper;
import cn.yougou.pojo.YgCategory;
import cn.yougou.service.shopcart.YgCategoryService;
import cn.yougou.vo.YgCategoryVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * (YgCategory)表服务实现类
 *
 * @author 白杰崇
 * @since 2020-12-03 20:34:52
 */
@Service
public class YgCategoryServiceImpl implements YgCategoryService {

    @Autowired
    private YgCategoryMapper ygCategoryMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public YgCategory queryById(Integer id) {
        return this.ygCategoryMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<YgCategory> queryAllByLimit(int offset, int limit) {
        return this.ygCategoryMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 查询三级分类
     * @param ygCategory 实例对象
     * @return
     */
    @Override
    public List<YgCategoryVo> categoryAll(YgCategory ygCategory) {
        List<YgCategoryVo> aTitle = null;
        if(ygCategory.getPid() == 0){
            aTitle = ygCategoryMapper.categoryAll(ygCategory);
            for (YgCategoryVo atitle : aTitle) {
                ygCategory.setPid(atitle.getId());
                List<YgCategoryVo> bTitle = ygCategoryMapper.categoryAll(ygCategory);
                atitle.setChildren(bTitle);
                for (YgCategoryVo btitle :bTitle){
                    ygCategory.setPid(btitle.getId());
                    btitle.setChildren(ygCategoryMapper.categoryAll(ygCategory));
                }
            }
            return aTitle;
        }else{
            return aTitle;
        }
    }

    /**
     * 商品列表页面根据一级标题id查询二级三级分类
     * 参数：二级分类pid
     * @return
     */
    @Override
    public List<YgCategoryVo> categoryById(YgCategory ygCategory) {
        List<YgCategoryVo> bTitle = null;
        List<YgCategoryVo> cTitle = null;
        if(ygCategory.getPid() != null){
            bTitle = ygCategoryMapper.categoryAll(ygCategory);
            for (YgCategoryVo btitle :bTitle) {
                ygCategory.setPid(btitle.getId());
                cTitle = ygCategoryMapper.categoryAll(ygCategory);
                btitle.setChildren(cTitle);
            }
        }
        return bTitle;
    }

    /**
     * 新增数据
     *
     * @param ygCategory 实例对象
     * @return 实例对象
     */
    @Override
    public YgCategory insert(YgCategory ygCategory) {
        this.ygCategoryMapper.insert(ygCategory);
        return ygCategory;
    }

    /**
     * 修改数据
     *
     * @param ygCategory 实例对象
     * @return 实例对象
     */
    @Override
    public YgCategory update(YgCategory ygCategory) {
        this.ygCategoryMapper.update(ygCategory);
        return this.queryById(ygCategory.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.ygCategoryMapper.deleteById(id) > 0;
    }

    /**
     * 查询商品id 和名称
     * */
    public List returnCategoryIDandNameList(){
        List<YgCategoryVo> list = ygCategoryMapper.categoryAll(new YgCategory("L3"));
        List data = new ArrayList();
        for(int i=0;i<list.size();i++){
            data.add(new YgCategory(list.get(i).getId(),list.get(i).getName()));
        }
        return data;
    }
}